import {
  Login,
  logout,
  getInfo
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  setid,
  settype,
  setlevel,
  getlevel,
  removelevel,
  setbid,
  getbid,
  setpid,
  getpid,
 getuid,
  setuid,
setrole,
  


} from '@/utils/auth'
import request from '@/utils/request'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password,
      usertype
    } = userInfo


    return new Promise((resolve, reject) => {
      Login({
        username: username.trim(),
        password: password,
        usertype: usertype
      }).then(response => {
        console.log(response)
        var data = response.data.object
        commit('SET_TOKEN', data.token)
        commit('SET_NAME', "Super Admin"),
        commit('SET_AVATAR', "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"),
        setToken(data.token)
        setid(data.cid);
        setbid(data.bid);
        setpid(data.pid);
        setrole(data.role)
        setlevel(data.level);
        setuid(data.id)
        console.log('login.....')
        console.log('level:',getlevel())
        console.log('uid:',getuid())
        // request(`person/rolelist`, {
        //     method: "get",
        //     headers: {
        //       "Content-Type": "application/json",
        //       token: data.token,
        //       type: 1
        //     },
        //     params: {tokens:data.token}
        //   })
        //   .then(res => {
        //     console.log(res);
        //     if(res.data.code==1){
        //     setid(res.data.object.cid);
        //     setlevel(res.data.object.level);
        //     console.log('set cid success,cid is',res.data.object.cid);
        //     console.log('set level success,level is',res.data.object.level)
        //     }else{
        //       console.log('set cid error!')
        //     }
        //   })
        //   .catch(e => {
        //     console.log(e);
        //   });
        
        settype(data.usertype)

        resolve(data)

      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  // getInfo({
  //   commit,
  //   state
  // }) {
  //   return new Promise((resolve, reject) => {
  //     getInfo(state.token).then(response => {
  //       response={
  //         avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
  //         introduction: "I am a super administrator",
  //         name: "Super Admin",
  //         0: "admin",
  //       }
  //       const {
  //         data
  //       } = response

  //       if (!data) {
  //         reject('Verification failed, please Login again.')
  //       }

  //       const {
  //         name,
  //         avatar
  //       } = data

  //       commit('SET_NAME', "Super Admin"),
  //       commit('SET_AVATAR', "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
  //       )
  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // user logout
  
  logout({
    commit,
    state
  }) {

    removeToken() // must remove  token  first
    resetRouter()
    commit('RESET_STATE')
     console.log('已经退出')
    // return new Promise((resolve, reject) => {
    //   logout(state.token).then(() => {
    //     removeToken() // must remove  token  first
    //     resetRouter()
    //     commit('RESET_STATE')
    //     resolve()
    //   }).catch(error => {
    //     reject(error)
    //   })
    // })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
